package com.rom.sort;

import com.rom.Utils.Random;

import java.util.Arrays;
import java.util.Date;

public class BubbleSort {

    public static void main(String[] args) {

//        Thu Nov 23 14:17:29 CST 2023
//        Thu Nov 23 14:19:24 CST 2023
//        int arr[] = {3,9,-1,10,2};
//        int arr[] = {3,2,1,4,5};
        int arr[] = new int[300000];
        System.out.println(new Date());
        for (int i = 0; i < 300000; i++) {
            arr[i] = Random.random80000();
        }
        bubbleSort(arr);
        System.out.println(new Date());
//        System.out.println(Arrays.toString(arr));
    }


    public static void bubbleSort(int[] arr){
        int temp = 0;
        boolean flag = false;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                //如果前面比后面的数字大  则交换
                if(arr[j]>arr[j + 1]){
                    flag = true;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
//            System.out.println("第"+(i+1)+"次排序："+Arrays.toString(arr));
            if(!flag){
                break;
            }else{
                flag = false;
            }
        }
    }
}
